/*
 * Copyright 2015-2025 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package fucksocks.client;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;

import fucksocks.common.SSLConfiguration;
import fucksocks.common.SSLConfigurationException;
import fucksocks.common.SocksException;

/**
 * The class <code>SSLSocks5</code> represents a SSL based SOCKS5 proxy. It will create a SSL based
 * connection between the client and SOCKS5 server.
 *
 * @author Youchao Feng
 * @date May 18, 2015 1:00:18 PM
 * @version 1.0
 * 
 * @see fucksocks.client.Socks5
 *
 */
public class SSLSocks5 extends Socks5 {

  /**
   * SSL configuration.
   */
  private SSLConfiguration configuration;

  public SSLSocks5(SocketAddress address, SSLConfiguration configuration) {
    super(address);
    this.configuration = configuration;
  }

  public SSLSocks5(InetAddress address, int port, SSLConfiguration configuration) {
    super(address, port);
    this.configuration = configuration;
  }

  @Override
  public Socket createProxySocket(InetAddress address, int port) throws IOException {
    try {
      return configuration.getSSLSocketFactory().createSocket(address, port);
    } catch (SSLConfigurationException e) {
      throw new SocksException(e.getMessage());
    }
  }



  @Override
  public Socket createProxySocket() throws IOException {
    try {
      return configuration.getSSLSocketFactory().createSocket();
    } catch (SSLConfigurationException e) {
      throw new SocksException(e.getMessage());
    }
  }

  @Override
  public SocksProxy copy() {
    return copyWithoutChainProxy().setChainProxy(getChainProxy());
  }

  @Override
  public SocksProxy copyWithoutChainProxy() {
    SSLSocks5 socks5 = new SSLSocks5(getInetAddress(), getPort(), configuration);
    socks5.setAcceptableMethods(getAcceptableMethods())
        .setAlwaysResolveAddressLocally(isAlwaysResolveAddressLocally())
        .setCredentials(getCredentials()).setInetAddress(getInetAddress()).setPort(getPort())
        .setSocksMethodRequestor(getSocksMethodRequestor());
    return socks5;
  }

  public SSLConfiguration getConfiguration() {
    return configuration;
  }

  public void setConfiguration(SSLConfiguration configuration) {
    this.configuration = configuration;
  }
}
